package com.example.spsaveqqty;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.spsaveqqty.travelguidesdatabase.DatabaseUtil;
import com.example.spsaveqqty.userManage.SaveUserInfo;
import com.example.spsaveqqty.userManage.User;
import com.example.spsaveqqty.userManage.UserDao;

import java.io.IOException;
import java.util.Map;
import com.example.spsaveqqty.guides.GuidesMainActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_number;
    private EditText et_password;
    private Button btn_login;
    private Button btn_reg;

    private UserDao userdao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);//判断是否有SD卡
        if(hasSDCard){
            copyDataBaseToPhone();//把准备好的数据库复制到手机里
        }else{
            Toast.makeText(getApplicationContext(), "未检测到SD卡", Toast.LENGTH_SHORT).show();
        }
        //初始化页面
        //创建userdao对象
        userdao = new UserDao(this);
        init();

        //获取注册页面传过来的flag值，若注册成功后应该不显示用户名和密码提示信息
        Intent intent = getIntent();
        String flag = intent.getStringExtra("flag");
        if(flag==null){//表示不是从注册成功后跳转过来的
            //加载保存成功的用户信息
            Map<String, String> userInfo = SaveUserInfo.getUserInfo(this);
            if (userInfo != null) {
                et_number.setText(userInfo.get("number"));
                et_password.setText(userInfo.get("password"));
            }
        }

    }
    private void init() {
        et_number = (EditText) findViewById(R.id.et_number);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_reg = (Button) findViewById(R.id.btn_reg);
        //设置按钮的点击事件
        btn_login.setOnClickListener(this);
        btn_reg.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login://登录
                //当点击登录按钮时,获取账号和密码
                String username = et_number.getText().toString().trim();
                String password = et_password.getText().toString().trim();
                //检验账号和密码是否输入正确
                if(!TextUtils.isEmpty(username)&&!TextUtils.isEmpty(password)&&username.length()<17&&password.length()<17){
                    //检验账号和密码是否存在
                    int count = userdao.getUserCountByUsernamePassword(username,password);
                    if(count==0){//代表此账号不存在
                        et_number.setText("");
                        et_password.setText("");
                        Toast.makeText(this, "请输入正确的账号和密码！", Toast.LENGTH_SHORT).show();
                    }else{//登录成功进入主页
                        //保存用户信息 以便下一次用户登录不用输入
                        SaveUserInfo.saveUserInfo(this, username, password);
                        //给全局变量USERNAME，USERID，USERPICTURE，FLAG赋值，方便后续页面使用：
                        User.USERID = userdao.getUserIdByUsername(username);
                        User.USERPICTURE = userdao.getUserPictureByUsername(username);
                        User.FLAG = userdao.getUserPictureFlagByUsername(username);
                        User.USERNAME = username;
                        //登录成功进入主页
                        //把username传递给个人中心页面：
                        Intent intent = new Intent(MainActivity.this, GuidesMainActivity.class);
                        intent.putExtra("username", username);
                        startActivity(intent);
                        finish();
                    }
                }else{
                    Toast.makeText(this, "请输入账号和密码，账号和密码均不能超过16位！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_reg://注册
                Intent intent = new Intent(MainActivity.this, RegActivity.class);
                startActivity(intent);
                Toast.makeText(this, "欢迎注册", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private void copyDataBaseToPhone() {
        DatabaseUtil util = new DatabaseUtil(this);
        // 判断数据库是否存在
        boolean dbExist = util.checkDataBase();
        if (dbExist) {
            Log.i("tag", "The database is exist.");
        } else {// 不存在就把raw目录下的数据库写入手机
            try {
                util.copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }
}
